<?
    function getDatafitting($xml_file) {
        global $datafit;
        //$objeto  = domxml_open_file($xml_file);
        //$raiz = $objeto->document_element();
        $objeto = new DOMDocument();
        $objeto->load($xml_file);
        $filho = $objeto->getelementsbytagname("datafitting");
        if ($filho->length > 0) {
            $datafit = $filho->item(0)->getelementsbytagname("datafit");
            return $datafit;
        }
        else return null;
    }

    function getPNumber($indice) {
        global $datafit, $p;
        $pnumber = $datafit->item($indice)->getelementsbytagname("num_points");
        if ($pnumber->length > 0) {
            $p = $pnumber->item(0)->nodeValue;
            return $p;
        }
        else return null;
    }

    function getDegree($indice) {
        global $datafit, $degree;
        $dg = $datafit->item($indice)->getelementsbytagname("degree");
        if ($dg->length > 0) {
            $degree = $dg->item(0)->nodeValue;
            return $degree;
        }
        else return null;
    }

    function getFunction($indice) {
        global $datafit, $f;
        $function = $datafit->item($indice)->getelementsbytagname("function");
        if ($function->length > 0) {
            $f = $function->item(0)->nodeValue;
            return $f;
        }
        else return null;
    }

    function getData($indice) {
        global $datafit, $data;
        $options = $datafit->item($indice)->getelementsbytagname("options");
        if ($options->length > 0) {
            $data = $options->item(0)->getattribute("data");
            return $data;
        }
        else return null;
    }

    function getPoints($indice) {
        global $datafit, $ptos;
        $points = $datafit->item($indice)->getelementsbytagname("point");
        if ($points->length > 0) {
            for($i = 0; $i < $points->length; $i++) {
                $x = $points->item($i)->getelementsbytagname("x");
                $ptos[2*$i] = $x->item(0)->nodeValue;
                $y = $points->item($i)->getelementsbytagname("y");
                $ptos[2*$i+1] = $y->item(0)->nodeValue;
            }
            return $ptos;
        }
        else return null;
    }


    function getInterval($indice) {
        global $datafit, $intvl;
        $interval = $datafit->item($indice)->getelementsbytagname("interval");
        if ($interval->length > 0) {
            for($i = 0; $i < $interval->length; $i++) {
                $x = $interval->item($i)->getelementsbytagname("x");
                $intvl[2*$i] = $x->item(0)->nodeValue;
                $y = $interval->item($i)->getelementsbytagname("y");
                $intvl[2*$i+1] = $y->item(0)->nodeValue;
            }
            return $intvl;
        }
        else return null;
    }

    function getDesc($indice) {
        global $datafit, $desc;
        $descricao = $datafit->item($indice)->getelementsbytagname("description");
        $desc = $descricao->item(0)->nodeValue;
        return $desc;
    }

    function getCom($indice) {
        global $datafit, $com;
        $comentario = $datafit->item($indice)->getelementsbytagname("comments");
        if ($comentario->length > 0) {
            $com = $comentario->item(0)->nodeValue;
            return $com;
        }
        else return null;
    }

    function getRef($indice) {
        global $datafit, $ref;
        $referencia = $datafit->item($indice)->getelementsbytagname("reference");
        if ($referencia->length>0) {
            $ref = $referencia->item(0)->nodeValue;
            return $ref;
        }
        else return null;
    }

    function createMenu() {
        global $datafit,$msg;
        for($i = 0; $i < $datafit->length; $i++) {
            $temp = getData($i); $temp1 = $i+1;
            if ($temp=="discrete") {
                echo "<a class=bibliotecaexemplos href=javascript:createLinks($i)><u>";
                echo $msg->get('XML_EXIBE_INTERPOLACAO_MSG01')."$temp1".$msg->get('XML_EXIBE_INTERPOLACAO_MSG02')." ";
                $temp = $msg->get("XML_EXIBE_INTERPOLACAO_MSG03");
                echo "$temp";
                echo "</u></a><p>";
            }
            else if ($temp=="continuous") {
                $temp = $msg->get("XML_EXIBE_INTERPOLACAO_MSG04");
            }
        }
        return;
    }
    if (empty($xml_file) && empty($id_bib)) $id_bib = "bib_demo.xml";
    if ( (!isset($indice)) || ($indice == null) ) { ?>
<p>
        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function createLinks(value) {
               document.myform.indice.value = value;
               document.myform.action = 'index.php?id_bib=<?echo $id_bib?>&inc=ajdados2.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

	  <? //$document_root = 'c:/adaptweb/adaptweb/html/anaweb';
      ?>
        <? if (isset($id_bib)) $xml_file = $DOCUMENT_ROOT."/bibliotecas/".$id_bib; ?>
        <? verificaBib(@$xml_file); ?>
        <?  if (file_exists($xml_file)) {
                $temp = getDatafitting($xml_file);
                if (!$temp==null) {
                   echo "<p>";
                   echo "<p align=left><font face=Arial>".$msg->get('XML_EXIBE_MSG04')."</font>";
                   echo "<p>";
                   createMenu();
                } else {
                     echo $msg->get('XML_EXIBE_MSG05');
                     echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
                }
            } else {
                    echo $msg->get('XML_EXIBE_MSG06');
                    echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
            }
 	?>

        <input type="hidden" name="id_bib" value="<? echo $id_bib ?>">
        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="indice">
        <input class="button" type="button" value="<? echo $msg->get('XML_EXIBE_MSG07'); ?>" onClick="history.back()">
        </form>

<? } else { ?>
<p>
        <? verificaBib(@$xml_file); ?>
        <form name="myform" enctype="multipart/form-data" method="post" onsubmit="sendToCalculus()">

        <script language="javascript" type="text/javascript">
            function sendToCalculus() {
               document.myform.action = 'index.php?id_bib=<?echo $id_bib?>&indice=<?echo $indice?>&inc=ajdados2.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

        <? getDatafitting($xml_file); ?>
        <? $dados = getData($indice);?>
        <? $funcao = getFunction($indice);?>
        <? $pontos = getPNumber($indice);?>
        <? $grau = getDegree($indice);?>
        <? $points = getPoints($indice);?>
        <? $intvl = getInterval($indice);?>
        <? $int1 = $intvl[0]; $int2 = $intvl[1]; ?>
        <? $desc = getDesc($indice); ?>
        <? $com = getCom($indice); ?>
        <? $ref = getRef($indice); ?>

        <? if (!strlen($desc) == 0) { ?>
              <p align="left"><font face="Arial"><b><?php echo $msg->get('Descricao') ?>: </b><? echo htmlentities(utf8_decode($desc)) ?></font>
        <? } ?>

        <input type="hidden" name="opcao_execucao" value="0">
        <p align="left"><font face="Arial"><?php echo $msg->get('XML_GERA_AJDADOS_MSG07') ?> </font>
        <input type="hidden" size='5' maxlength='5' name="pontos" value="<? echo $pontos ?>"><? echo $pontos; ?>
		<p align="left"><font face="Arial"><?php echo $msg->get('XML_GERA_AJDADOS_MSG08') ?></font>
		<br><br>
		<table>
			<tr>
			   <td align="center"><font face="Arial">x</font></td>
			   <td align="center"><b>,</b></td>
			   <td align="center"><font face="Arial">y</font></td>
			</tr>
			<? for ($i=0; $i<$pontos; $i++) { ?>
            <tr>
                <td align="center">( <? echo $points[2*$i]; ?><input type="hidden" size='5' maxlength='30' name="vet[<? echo $i ?>][0]" value="<? echo $points[2*$i] ?>"></td>
                <td align="center"><b>,</b></td>
                <td align="center"><? echo $points[2*$i+1]; ?><input type="hidden" size='5' maxlength='30' name="vet[<? echo $i ?>][1]" value="<? echo $points[2*$i+1] ?>"> )</td>
            </tr>
			<? } ?>
		</table>
        <p align="left"><font face="Arial"><?php echo $msg->get('XML_GERA_AJDADOS_MSG09') ?></font>
		<select name="grau">
		<? for ($i = 0; $i < $pontos; $i++) { ?>
		<option value="<? echo $i ?>"<? echo ($grau == $i)?"selected=selected":""; ?>><? echo $i; ?></option>
		<? } ?>
		</select>
		<input type="hidden" name="Calcular" value="sim">
		<p>
        <?php echo $msg->get('Informe_X') ?>:<br>
        <input type="text" name="calcx" size="5"><br>
		<p>
        <? if (!$com == null) { ?>
              <p align="left"><font face="Arial"><b><?php echo $msg->get("XML_EXIBE_MSG08") ?> </b><? echo htmlentities(utf8_decode($com)) ?></font>
        <? } ?>
        <p>
        <? if (!$ref == null) { ?>
              <p align="left"><font face="Arial"><b><?php echo $msg->get("XML_EXIBE_MSG09") ?> </b><? echo htmlentities(utf8_decode($ref)) ?></font>
        <? } ?>
        <p>

        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="executar" value="sim">
        <input type="hidden" name="navegacao" value="sim">
        <input class="button" type="button" value="Proximo >>" onClick="sendToCalculus()"><p>
        </form>
<?
    echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
    echo "<script language=\"javascript\" type=\"text/javascript\">\n";
    echo "function criarLinks(value) {\n";
    echo "document.exemplos.indice.value = value;\n";
    echo "document.exemplos.action = 'index.php?inc=ajdados2.php&modo=exemplo'\n";
    echo "document.exemplos.submit();\n";
    echo "}\n";
    echo "</script>";
    echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
    echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
    echo "<input type=\"hidden\" name=\"indice\">";
    echo "</form>";
    echo $msg->get('Outros_Exemplos').":<br>";
    $objeto = new DOMDocument();
    $objeto->load($xml_file);
    $filho = $objeto->getelementsbytagname("datafitting");
    if ($filho->length > 0)
    {
        $datafit = $filho->item(0)->getelementsbytagname("datafit");
        for ($i=0; $i<$datafit->length; $i++)
        {
            $options = $datafit->item($i)->getelementsbytagname("options");
            if ($options->length > 0) {
                $data = $options->item(0)->getattribute("data");
            }
            if ($data == "discrete")
            {
                if ($i != 0) echo " | ";
                if ($i == $indice) echo "<b>";
                echo "<a href=\"javascript:criarLinks($i)\"><u>".($i+1)."</u></a>";
                if ($i == $indice) echo "</b>";
            }
        }
    }
?>
<? } ?>